package it.appcatalogobase;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ProductDettaglioActivity extends ActionBarActivity {
	private String type;
	private String id;
	int counterNext;
	int counterPrev;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product_dettaglio);

		// if (savedInstanceState == null) {
		// getSupportFragmentManager().beginTransaction()
		// .add(R.id.container, new PlaceholderFragment()).commit();
		// }

		counterNext = 0;
		counterPrev = 0;

		Intent i = getIntent();
		String name = i.getStringExtra("name");
		String des = i.getStringExtra("des");
		String imm = i.getStringExtra("imm");
		id = i.getStringExtra("id");
		String colori = i.getStringExtra("colori");
		type = i.getStringExtra("type");
		if (type.equals("single")) {
			ImageView immNext = (ImageView) findViewById(R.id.next);
			ImageView immPrev = (ImageView) findViewById(R.id.prev);
			immNext.setVisibility(View.GONE);
			immPrev.setVisibility(View.GONE);
		}
		TextView tNome = (TextView) findViewById(R.id.nome);
		tNome.setText(name);
		TextView tDes = (TextView) findViewById(R.id.descrizione);
		String des1 = "'<p style='text-align: center;'><span style='font-size: 12pt; color: #ff0000;'><strong>Frog Revolution </strong></span></p>\r\n<p><span style='font-size: 10pt;'>Un mix perfetto di design e praticit&agrave;, che stimola il piacere dello sguardo.</span></p>\r\n<p><span style='font-size: 10pt;'>La Frog &egrave; una macchina da caff&egrave; espresso a cialde destinata sia al mercato domestico che quello aziendale. </span></p>\r\n<p><span style='font-size: 10pt;'>Il suo design innovativo, la sua semplicit&agrave; e la qualit&agrave; delle sue componenti, la rendono un bene strumentale affidabile a cui puntare in tutta tranquillit&agrave;. </span></p>\r\n<p><span style='font-size: 10pt;'><strong>DIMENSIONI:</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Larghezza: 22 cm </span></li>\r\n<li><span style='font-size: 10pt;'>Altezza: 40 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Profondit&agrave;: 32 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Peso: 6.5 Kg</span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>CARATTERISTICHE TECNICHE</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Potenza normale 650 W </span></li>\r\n<li><span style='font-size: 10pt;'>Voltaggio 230 V</span></li>\r\n<li><span style='font-size: 10pt;'>Sistema di decalcificazione rapido </span></li>\r\n<li><span style='font-size: 10pt;'>Alloggio bottiglia d'acqua </span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>ACCESSORI</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Vano porta cialde e bicchieri</span></li>\r\n</ul>";
		des = des.replace("\\/", "/");
		des = des.replace("\\", "/");
		des.replace("/r", "<br/>");
		des.replace("/n", "<br/>");
		des = des.replace("\"", "'");
		des = des.replace("&lt;", "<");
		des = des.replace("&gt;", ">");
		TextView txtCol = (TextView) findViewById(R.id.textViewColori);
		LinearLayout layoutCol = (LinearLayout) findViewById(R.id.layoutColori);

		if (colori != null) {
			String[] colors = colori.split(",");
			txtCol.setVisibility(View.VISIBLE);
			layoutCol.setVisibility(View.VISIBLE);
			LayoutParams lp = new LayoutParams(25, 25);
			lp.setMargins(0, 0, 20, 0);
			layoutCol.removeAllViewsInLayout();
			for (String color : colors) {
				View v = new View(this);
				v.setLayoutParams(lp);
				Drawable d = getResources().getDrawable(R.drawable.rect);
				d.setColorFilter(Color.parseColor("#" + color.trim()), Mode.MULTIPLY);
				v.setBackground(d);
				layoutCol.addView(v);
			}
		} else {
			txtCol.setVisibility(View.GONE);
			layoutCol.setVisibility(View.GONE);
		}
		Log.v("prova", des);

		// des="<![CDATA["+des+"]]>";
		// Properties p = new Properties();
		// try {
		// p.load(getAssets().open("config.properties"));
		// } catch (IOException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		// p.setProperty("value2", "<![CDATA["+des+"]]>");
		System.out.println(des);
		try {
			tDes.setText(URLDecoder.decode(Html.fromHtml(des).toString(), "ISO-8859-1"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ImageView immV = (ImageView) findViewById(R.id.immagine);
		Bitmap bm = null;
		try {
			bm = BitmapFactory.decodeStream(openFileInput(imm));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		immV.setImageBitmap(bm);
	}

	public void next(View v) {
		ProdottoDataSource datasourcepr = new ProdottoDataSource(this);
		datasourcepr.open();
		List<Prodotto> prodotti;
		if (type.equals("cat"))
			prodotti = datasourcepr.getAllProdottiFromCat(datasourcepr.getProdotto(Long.valueOf(id)).getCategoria());
		else
			prodotti = datasourcepr.getAllProdotti();
		Prodotto pr = null;
		int indexFound = 0;
		for (int i = 0; i < prodotti.size(); i++) {
			if (prodotti.get(i).getId() == Long.valueOf(id)) {
				indexFound = i;
				i = prodotti.size();
			}
		}
		if (indexFound != prodotti.size() - 1)
			pr = prodotti.get(indexFound + 1);
		else
			pr = prodotti.get(0);

		if (pr != null) {
			counterNext++;
			id = String.valueOf(pr.getId());
			// Intent i = new Intent(ProductDettaglioActivity.this,
			// ProductDettaglioActivity.class);
			// i.putExtra("id", String.valueOf(pr.getId()));
			// i.putExtra("name", pr.getNome());
			// i.putExtra("des", pr.getDescrizione());
			// i.putExtra("imm", pr.getImmagine());
			// i.putExtra("type", "all");
			// i.putExtra("colori", pr.getColori());
			// startActivity(i);
			TextView tNome = (TextView) findViewById(R.id.nome);
			tNome.setText(pr.getNome());
			TextView tDes = (TextView) findViewById(R.id.descrizione);
			String des1 = "'<p style='text-align: center;'><span style='font-size: 12pt; color: #ff0000;'><strong>Frog Revolution </strong></span></p>\r\n<p><span style='font-size: 10pt;'>Un mix perfetto di design e praticit&agrave;, che stimola il piacere dello sguardo.</span></p>\r\n<p><span style='font-size: 10pt;'>La Frog &egrave; una macchina da caff&egrave; espresso a cialde destinata sia al mercato domestico che quello aziendale. </span></p>\r\n<p><span style='font-size: 10pt;'>Il suo design innovativo, la sua semplicit&agrave; e la qualit&agrave; delle sue componenti, la rendono un bene strumentale affidabile a cui puntare in tutta tranquillit&agrave;. </span></p>\r\n<p><span style='font-size: 10pt;'><strong>DIMENSIONI:</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Larghezza: 22 cm </span></li>\r\n<li><span style='font-size: 10pt;'>Altezza: 40 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Profondit&agrave;: 32 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Peso: 6.5 Kg</span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>CARATTERISTICHE TECNICHE</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Potenza normale 650 W </span></li>\r\n<li><span style='font-size: 10pt;'>Voltaggio 230 V</span></li>\r\n<li><span style='font-size: 10pt;'>Sistema di decalcificazione rapido </span></li>\r\n<li><span style='font-size: 10pt;'>Alloggio bottiglia d'acqua </span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>ACCESSORI</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Vano porta cialde e bicchieri</span></li>\r\n</ul>";
			String des = pr.getDescrizione().replace("\\/", "/");
			des = des.replace("\\", "/");
			des.replace("/r", "<br/>");
			des.replace("/n", "<br/>");
			des = des.replace("\"", "'");
			des = des.replace("&lt;", "<");
			des = des.replace("&gt;", ">");
			TextView txtCol = (TextView) findViewById(R.id.textViewColori);
			LinearLayout layoutCol = (LinearLayout) findViewById(R.id.layoutColori);

			if (pr.getColori() != null) {
				String[] colors = pr.getColori().split(",");
				txtCol.setVisibility(View.VISIBLE);
				layoutCol.setVisibility(View.VISIBLE);
				layoutCol.removeAllViewsInLayout();
				LayoutParams lp = new LayoutParams(25, 25);
				lp.setMargins(0, 0, 20, 0);
				for (String color : colors) {
					View v1 = new View(this);
					v1.setLayoutParams(lp);
					Drawable d = getResources().getDrawable(R.drawable.rect);
					d.setColorFilter(Color.parseColor("#" + color.trim()), Mode.MULTIPLY);
					v1.setBackground(d);
					layoutCol.addView(v1);
				}
			} else {
				txtCol.setVisibility(View.GONE);
				layoutCol.setVisibility(View.GONE);
			}
			Log.v("prova", des);

			// des="<![CDATA["+des+"]]>";
			// Properties p = new Properties();
			// try {
			// p.load(getAssets().open("config.properties"));
			// } catch (IOException e1) {
			// // TODO Auto-generated catch block
			// e1.printStackTrace();
			// }
			// p.setProperty("value2", "<![CDATA["+des+"]]>");
			System.out.println(des);
			try {
				tDes.setText(URLDecoder.decode(Html.fromHtml(des).toString(), "ISO-8859-1"));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			ImageView immV = (ImageView) findViewById(R.id.immagine);
			Bitmap bm = null;
			try {
				bm = BitmapFactory.decodeStream(openFileInput(pr.getImmagine()));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			immV.setImageBitmap(bm);

		}
	}

	public void prev(View v) {
		ProdottoDataSource datasourcepr = new ProdottoDataSource(this);
		datasourcepr.open();
		List<Prodotto> prodotti;
		if (type.equals("cat"))
			prodotti = datasourcepr.getAllProdottiFromCat(datasourcepr.getProdotto(Long.valueOf(id)).getCategoria());
		else
			prodotti = datasourcepr.getAllProdotti();
		Prodotto pr = null;
		int indexFound = 0;
		for (int i = 0; i < prodotti.size(); i++) {
			if (prodotti.get(i).getId() == Long.valueOf(id)) {
				indexFound = i;
				i = prodotti.size();
			}
		}
		if (indexFound != 0)
			pr = prodotti.get(indexFound - 1);
		else
			pr = prodotti.get(prodotti.size() - 1);

		if (pr != null) {
			counterNext++;
			id = String.valueOf(pr.getId());
			// Intent i = new Intent(ProductDettaglioActivity.this,
			// ProductDettaglioActivity.class);
			// i.putExtra("id", String.valueOf(pr.getId()));
			// i.putExtra("name", pr.getNome());
			// i.putExtra("des", pr.getDescrizione());
			// i.putExtra("imm", pr.getImmagine());
			// i.putExtra("type", "all");
			// i.putExtra("colori", pr.getColori());
			// startActivity(i);
			TextView tNome = (TextView) findViewById(R.id.nome);
			tNome.setText(pr.getNome());
			TextView tDes = (TextView) findViewById(R.id.descrizione);
			String des1 = "'<p style='text-align: center;'><span style='font-size: 12pt; color: #ff0000;'><strong>Frog Revolution </strong></span></p>\r\n<p><span style='font-size: 10pt;'>Un mix perfetto di design e praticit&agrave;, che stimola il piacere dello sguardo.</span></p>\r\n<p><span style='font-size: 10pt;'>La Frog &egrave; una macchina da caff&egrave; espresso a cialde destinata sia al mercato domestico che quello aziendale. </span></p>\r\n<p><span style='font-size: 10pt;'>Il suo design innovativo, la sua semplicit&agrave; e la qualit&agrave; delle sue componenti, la rendono un bene strumentale affidabile a cui puntare in tutta tranquillit&agrave;. </span></p>\r\n<p><span style='font-size: 10pt;'><strong>DIMENSIONI:</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Larghezza: 22 cm </span></li>\r\n<li><span style='font-size: 10pt;'>Altezza: 40 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Profondit&agrave;: 32 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Peso: 6.5 Kg</span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>CARATTERISTICHE TECNICHE</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Potenza normale 650 W </span></li>\r\n<li><span style='font-size: 10pt;'>Voltaggio 230 V</span></li>\r\n<li><span style='font-size: 10pt;'>Sistema di decalcificazione rapido </span></li>\r\n<li><span style='font-size: 10pt;'>Alloggio bottiglia d'acqua </span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>ACCESSORI</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Vano porta cialde e bicchieri</span></li>\r\n</ul>";
			String des = pr.getDescrizione().replace("\\/", "/");
			des = des.replace("\\", "/");
			des.replace("/r", "<br/>");
			des.replace("/n", "<br/>");
			des = des.replace("\"", "'");
			des = des.replace("&lt;", "<");
			des = des.replace("&gt;", ">");
			TextView txtCol = (TextView) findViewById(R.id.textViewColori);
			LinearLayout layoutCol = (LinearLayout) findViewById(R.id.layoutColori);

			if (pr.getColori() != null) {
				String[] colors = pr.getColori().split(",");
				txtCol.setVisibility(View.VISIBLE);
				layoutCol.setVisibility(View.VISIBLE);
				LayoutParams lp = new LayoutParams(25, 25);
				lp.setMargins(0, 0, 20, 0);
				layoutCol.removeAllViewsInLayout();
				for (String color : colors) {
					View v1 = new View(this);
					v1.setLayoutParams(lp);
					Drawable d = getResources().getDrawable(R.drawable.rect);
					d.setColorFilter(Color.parseColor("#" + color.trim()), Mode.MULTIPLY);
					v1.setBackground(d);
					layoutCol.addView(v1);
				}
			} else {
				txtCol.setVisibility(View.GONE);
				layoutCol.setVisibility(View.GONE);
			}
			Log.v("prova", des);

			// des="<![CDATA["+des+"]]>";
			// Properties p = new Properties();
			// try {
			// p.load(getAssets().open("config.properties"));
			// } catch (IOException e1) {
			// // TODO Auto-generated catch block
			// e1.printStackTrace();
			// }
			// p.setProperty("value2", "<![CDATA["+des+"]]>");
			System.out.println(des);
			try {
				tDes.setText(URLDecoder.decode(Html.fromHtml(des).toString(), "ISO-8859-1"));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			ImageView immV = (ImageView) findViewById(R.id.immagine);
			Bitmap bm = null;
			try {
				bm = BitmapFactory.decodeStream(openFileInput(pr.getImmagine()));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			immV.setImageBitmap(bm);

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.product_dettaglio, menu);
		return true;
	}
	
	
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		// if (id == R.id.action_settings) {
		// return true;
		// }
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	// public static class PlaceholderFragment extends Fragment {
	//
	// public PlaceholderFragment() {
	// }
	//
	// @Override
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	// View rootView = inflater.inflate(
	// R.layout.fragment_product_dettaglio, container, false);
	// return rootView;
	// }
	// }

}
